package com.jctrip.hotel.source.xxl.dossen.thread;

import com.jctrip.hotel.common.constant.RedisKeyConstant;
import com.jctrip.hotel.common.context.BeanContext;
import com.jctrip.hotel.common.util.RedisUtil;
import com.jctrip.hotel.source.service.dossen.DossenService;
import com.jctrip.hotel.source.service.dossen.DossenTaskService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;

/**
 * @Author: zxy
 * @CreateTime: 2023-09-04 20:43
 * @Description:
 */
@Slf4j
public class SyncDossenLongerRatesTaskThread implements Runnable {

    private final String appId;
    private final String key;
    private final Integer days;
    private final Integer cardLevel;
    private final String activityCode;
    private final Integer addDays;

    public SyncDossenLongerRatesTaskThread(String appId, String key, Integer addDays,
                                           Integer days, Integer cardLevel, String activityCode) {
        this.appId = appId;
        this.key = key;
        this.days = days;
        this.cardLevel = cardLevel;
        this.activityCode = activityCode;
        this.addDays = addDays;
    }

    @Override
    public void run() {
        try {
            RedisUtil redisUtil = BeanContext.getApplicationContext().getBean(RedisUtil.class);
            DossenTaskService dossenTaskService = BeanContext.getApplicationContext().getBean(DossenTaskService.class);
            DossenService dossenService = BeanContext.getApplicationContext().getBean(DossenService.class);

            while (redisUtil.lGetListSize(RedisKeyConstant.HOTEL_MAIN_DOSSEN_LONGER_RATES_LIST) > 0) {
                try {
                    String hotelId = (String) redisUtil.rightPop(RedisKeyConstant.HOTEL_MAIN_DOSSEN_LONGER_RATES_LIST);
                    if (StringUtils.isBlank(hotelId)) {
                        continue;
                    }
                    Date start = DateUtils.addDays(new Date(), addDays);
                    Date end = DateUtils.addDays(start, days);
                    dossenTaskService.syncDossenRatesTaskPop(appId, key, dossenService.parseHotelId(hotelId), start,
                            end, cardLevel, activityCode);
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }
}